

def parent_function(person,coins):
    def play_game():
        # 不在play_game方法中定义局部变量
        # 直接使用父方法的变量
        nonlocal coins
        coins -= 1

        if coins > 1:
            print("\n" + person + " has " + str(coins) + " coins left.")
        elif coins == 1:
            print("\n" + person + " has " + str(coins) + " coin left.")
        else:
            print("\n" + person + " is out of coins.")
    
    return play_game

tommy = parent_function("tommy",3)
jenny = parent_function("jenny",5)

tommy()
tommy()
jenny()
tommy()

"""输出
tommy has 2 coins left.
tommy has 1 coin left.
jenny has 4 coins left.
tommy is out of coins.
"""